package org.unt.parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.unt.genlogic.Files;

public class DirStructureParser {

	public static void main(String[] args) {

		// new DirStructureParser().parse("C:/Speeder/workspace.12.1/SpdJava");
		// new DirStructureParser().parse("C:/Speeder/workspace.12.1/SpdWeb");

		List<File> outputFilesList = new ArrayList<File>();
		Set<String> extensions = new HashSet<String>();
		extensions.add("java");
		extensions.add("xml");
		Files.listFilesRecursively(new File("C:/IDVS/workspace2"), outputFilesList, extensions);

		String results = search("maven-checkstyle-plugin", outputFilesList);
		System.out.println(results);
	}

	public static String search(String token, List<File> files) {

		StringBuffer buf = new StringBuffer();

		for (File file : files) {

			BufferedReader reader;
			try {
				reader = new BufferedReader(new FileReader(file));
			} catch (FileNotFoundException e) {
				throw new RuntimeException("FileNotFoundException: cannot create FileReader from " + file.getAbsolutePath(), e);
			}

			String line;

			try {
				while ((line = reader.readLine()) != null) {

					if (line.indexOf(token) > -1) {
						buf.append(file.toString() + "\t" + file.getName() + "\t" + line.trim() + "\n");
					}
				}
			} catch (IOException e) {
				throw new RuntimeException("IOException: cannot read file " + file.getAbsolutePath(), e);
			}

		}

		if (buf.length() > 0)
			return buf.toString();

		return "Nothing found for " + token;
	}

	void parse(String baseDir) throws IOException {

		List beans = new ArrayList();

		beans.add("PR.listeFiliale");
		beans.add("historique.date");
		beans.add("general.piece.not.exist.in.stock");
		beans.add("menu.interdepannage.adv.masse1500");
		beans.add("propositioncommande.clientNonAdherantCmdStk");
		beans.add("interstock.onglet.pays.C");
		beans.add("client.onglet.delais");
		beans.add("interstock.infoMasse.piedsPage1500");
		beans.add("general.piece.not.exist.in.stock");
		beans.add("interstock.interlocuteur");
		beans.add("conseildestockage.tittle.createinfo");
		beans.add("client.onglet.delays.adhesion");
		beans.add("test.spd.libelle.word.8");
		beans.add("client.onglet.delays");
		beans.add("test.spd.libelle.word.123");
		beans.add("interstock.onglet.pays");
		beans.add("error.dopr.not.belong.sector");
		beans.add("interstock.onglet.autre");
		beans.add("test.for.pascal");

		File dir = new File(baseDir);

		List filesList = new ArrayList();

		Files.listFilesRecursively(dir, filesList, getBannedFiles(), getBannedExtensions(), getBannedDirs());

		System.out.println(filesList.size());

		for (Iterator iterator = filesList.iterator(); iterator.hasNext();) {

			File file = (File) iterator.next();

			BufferedReader reader = new BufferedReader(new FileReader(file));

			String line;
			while ((line = reader.readLine()) != null) {

				for (Iterator iterator2 = beans.iterator(); iterator2.hasNext();) {

					String bean = (String) iterator2.next();

					if (line.indexOf(bean) > -1) {

						System.out.println(bean + "\t" + file.getAbsolutePath());
					}
				}
			}
		}
	}

	public static Set getBannedFiles() {

		Set set = new HashSet();

		set.add("OJB-logging.properties");

		return set;
	}

	public static Set getBannedDirs() {

		Set set = new HashSet();

		set.add("target");
		set.add("CVS");
		set.add("bin");

		return set;
	}

	public static Set getBannedExtensions() {

		Set set = new HashSet();

		set.add("sql");

		set.add("sh");
		set.add("js");
		set.add("apt");
		set.add("tld");
		set.add("xsd");
		set.add("css");
		set.add("xsd");
		set.add("jar");
		set.add("bat");
		set.add("doc");
		set.add("ser");
		set.add("class");
		set.add("checkstyle");
		set.add("prefs");
		set.add("htm");
		set.add("html");
		set.add("classpath");
		set.add("classpath1");
		set.add("MF");
		set.add("cvsignore");
		set.add("dtd");
		set.add("djunitplugin");
		set.add("1");
		set.add("launch");
		set.add("project");
		set.add("component");
		set.add("pbd");

		return set;
	}
}
